using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
    public class tbKhachHangDAL
    {
        private SqlConnection cn = null;
        public tbKhachHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);           
            cn.Open();
        }
        public void Insert(tbKhachHang kh)
        {
            SqlCommand cm = new SqlCommand("sp_KhachHang_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@HoTen", kh.HoTen);
            cm.Parameters.AddWithValue("@Email", kh.Email);
            cm.Parameters.AddWithValue("@MatKhau", kh.MatKhau);
            cm.Parameters.AddWithValue("@DiaChi", kh.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", kh.DienThoai);
            cm.ExecuteNonQuery();

        }
        public void Update(tbKhachHang kh)
        {
            SqlCommand cm = new SqlCommand("sp_KhachHang_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdKhachHang", kh.IdKhachHang);
            cm.Parameters.AddWithValue("@HoTen", kh.HoTen);
            cm.Parameters.AddWithValue("@Email", kh.Email);
            cm.Parameters.AddWithValue("@MatKhau", kh.MatKhau);
            cm.Parameters.AddWithValue("@DiaChi", kh.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", kh.DienThoai);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_KhachHang_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdKhachHang", Id);
            cm.ExecuteNonQuery();
        }
        public tbKhachHangCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_KhachHang_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
          tbKhachHangCollection khcollection = new tbKhachHangCollection();
            while (dr.Read())
            {
                tbKhachHang kh = new tbKhachHang();
                kh.IdKhachHang = int.Parse(dr.GetValue(0).ToString());
                kh.HoTen = dr.GetValue(1).ToString();
                kh.Email = dr.GetValue(2).ToString();
                kh.MatKhau = dr.GetValue(3).ToString();
                kh.DiaChi = dr.GetValue(4).ToString();
                kh.DienThoai = dr.GetValue(5).ToString();
                khcollection.Add(kh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return khcollection;
        }
    }
}
